﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Configuration;
using System.Collections;
using System.Xml;
using Pub;
using Pub.Lib;
using Pub.Tools;
using System.IO;

namespace RemotingSerApp
{
    public partial class RemotingSerAppFrm : Form
    {
        public RemotingSerAppFrm()
        {
            InitializeComponent();
        }

        private TcpChannel tcpChannel;

        private TcpServerChannel tcpServerChannel;

        string strPort = string.Empty;
        string strMess = string.Empty;
          

        private void WriteLog(string cLog)
        {

        //    string FileDir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase.ToString();
       //     string FileName = @FileDir + "log.xml";

            string FileName = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase.ToString(), @"xml\log.xml");

            if (!System.IO.File.Exists(@FileName))
            { return; }

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@FileName);
            DataTable dtt = XmlDB.XmlToDataTable(xmlDoc.OuterXml);
            DataRow dr = dtt.NewRow();
            dr["dDate"] = System.DateTime.Now;
            dr["cText"] = cLog;

            dtt.Rows.Add(dr);

            xmlDoc.LoadXml(XmlDB.DataToXml(dtt));

            xmlDoc.Save(@FileName);
        }


        private void FrmServer_Load(object sender, EventArgs e)
        {
            strPort = XmlTools.Read(PubVar.SysSetXml, "SysSet/ServerPort");
            PubVar.GetSYSDAT();
            try
            {
                PubVar.Domain = Helper.DBHelper.GetInf("domain", PubVar.ConnMain, DataAccessType.CS).Trim();
            }
            catch (Exception ex)
            {
                SysTools.WriteTxt(PubVar.LogFile, "读取domain出错:" + ex.Message.ToString());
            }
            string _UpdateSQL = XmlTools.Read(PubVar.SysSetXml, "SysSet/UpdateSQL");

            PubVar.SysUpdateSQL = SysTools.ToBoolean(_UpdateSQL);

        }


        private void btnReg_Click(object sender, EventArgs e)
        {
            strMess = Pub.Lib.RemotingServer.Start(ref tcpServerChannel, strPort);
            labMess.Text = strMess;           
            btnUnReg.Enabled = true;
        }

        private void btnUnReg_Click(object sender, EventArgs e)
        {
            ChannelServices.UnregisterChannel(tcpServerChannel);
            labMess.Text = "Remoting 端口" + strPort + "(普通模式)服务关闭";
        }


        private void btnSecure_Click(object sender, EventArgs e)
        {
            strMess = Pub.Lib.RemotingServer.SecureStart(ref tcpChannel, strPort);
            labMess.Text = strMess;   
            btnUnSecure.Enabled = true;
        }

        private void btnUnSecure_Click(object sender, EventArgs e)
        {
            ChannelServices.UnregisterChannel(tcpChannel);
            labMess.Text = "Remoting 端口" + strPort + "(用户认证模式)服务关闭";
        }

    }
}
